Java Sessions, AWT Contexts, and Frames
To run Java applets on the Mac OS platform, you must first create a Java runtime session, which is an instantiation of the Java runtime environment. This environment can then load and execute Java code. Figure 1-2 shows an instantiated Java session and elements associated with it.Figure 1-2 An instantiated Java session
Within a session, a Java applet must run within an Abstract Window Toolkit (AWT) context. The AWT context provides an execution environment and a thread group for the Java program. Each Java applet must have its own AWT context. However, a given session can contain multiple contexts, each of which runs independently.
In addition to providing an execution environment, the AWT context allows access to the Abstract Window Toolkit. Java programs that display any graphical information (such as a text window, a button, or an image) must do so by calling the AWT. A call to the AWT manipulates images in a virtual window called a frame. This action is analogous to a Mac OS program calling the Mac OS Toolbox to manipulate images in a user-visible screen window.
To make the contents of a frame visible to the user, JManager cooperates with the AWT to pass information between the Java program and the Mac OS. For example, if the Java program decides to open a window, it tells the AWT, which can then call the appropriate Mac OS Toolbox functions to display a new window. Similarly, if the user selects a button, the selection is passed back to the Java program through the AWT. Figure 1-3 compares the elements needed by Mac OS code and Java code to display graphical information in a Mac OS window.
Figure 1-3 Frames versus windows
A Java applet can have any number of frames. A frame does not necessarily have to correspond to a visible Mac OS window. For example, you could assign a frame to a graphics port that displays data on a pen plotter instead of a window.
Since the Java runtime environment is object-oriented, each element associated with it is defined by an object. For example, an AWT context is defined by the
JMAWTContextRef
object, and the Java session itself is defined by an object of typeJMSessionRef
. Creating any element (for example, a frame) involves instantiating an instance of that object type.To run a Java applet on the Mac OS, you must do the following:
- Create the Java runtime environment.
- Find the applet.
- Create an AWT context.
- Instantiate the applet.